package com.taotao.controller;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.taotao.apitype.ApiHepler;
import com.taotao.apitype.ApiHepler.MessageResult;
import com.taotao.apitype.ApiHepler.PagingResponse;
import com.taotao.domain.TbOrder;
import com.taotao.service.impl.OrderImpl;
import com.taotao.viewmodel.OrderRequest;

@RestController
public class MainController {
	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private DiscoveryClient discoveryClient;
	@Autowired
	private OrderImpl orderImpl;

	@RequestMapping(path = "/orderList", method = RequestMethod.GET)
	public ApiHepler.PagingResponse<TbOrder> orderList(@RequestParam Map<String, Object> data) throws Exception {
		PagingResponse<TbOrder> list = orderImpl.orderList(data);
		return list;
	}

	@RequestMapping(path = "/orderSave", method = RequestMethod.POST)
	public ApiHepler.MessageResult SaveUser(@RequestBody OrderRequest data) throws Exception {
		MessageResult result = orderImpl.orderSave(data);
		return result;
	}
	
    @RequestMapping(value = "/add" ,method = RequestMethod.GET)
    public String add(@RequestParam Integer a, @RequestParam Integer b) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        Integer r = a + b;
        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
        return "From Service-A, Result is " + r;
    }

	@RequestMapping("/")
	public String sayhello() {
		return "欢迎进入订单系统！";
	}

}
